<?php
class links_request
{
	var $mydirname;
	var $controller_name;
	var $Links;

	/** Constructor
	 *
	**/
	function links_request($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		if (!$this->Links->request_perm) {
			$this->Links->linksRedirectHeader(XOOPS_URL . '/user.php', 2, _NOPERM);
			exit();
		}
	}

	function index($lid = 0)
	{
		$this->form($lid);
	}

	function form($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {

			$bread = $this->Links->getBreadcrumbs();
			$bread[] = array('name' => $link['title']);
			$data = array(
				'link'              => $link,
				'xoops_breadcrumbs' => $bread,
				'gticket'           => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_request'),
			);
			$template = $this->Links->getTemplatePrefix() . '_request.html';
			$this->Links->view($template, $data);

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function post()
	{
		$js_check = $this->Links->is_mobile ? FALSE: TRUE;
		$this->Links->basicPostCheck('links_request', $js_check);

		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		$return_msg = $this->_post_common($lid);

		if (empty($return_msg)) {
			$this->Links->linksRedirectHeader($this->Links->makeUrl('index', 'item', array('lid' => $lid)), 2, _MD_THANKSFORINFO);
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, $return_msg);
		}

	}

	function _post_common($lid)
	{
		$msg = '';

		if ($link = $this->Links->getDataByLid($lid)) {

			$db_model = $this->Links->setDbModel($this->controller_name);

			if ($this->Links->is_mobile && $this->Links->uid == 0) {
				$ip = xoops_getenv("REMOTE_ADDR");
				if (empty($ip)) {
					$msg = 'Request Error: '._MD_ANY_PROBLEM;
					return $msg;
				}
			}

			if ($db_model->insert($lid)) {
				return $msg;
			} else {
				$msg = _ERRORS;
				return $msg;
			}

		} else {
			$msg = _MD_CANT_GET_DATA;
			return $msg;
		}
	}

	// for javascript
	function post_j()
	{
		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		//Check Referer
		if (! (strpos(xoops_getenv('HTTP_REFERER'),XOOPS_URL) === 0)) {
			die('Illegal Referer');
		}

		if (! $GLOBALS['xoopsGTicket']->check(TRUE, 'links_request', FALSE)) {
			$new_gticket = $GLOBALS['xoopsGTicket']->issue(__LINE__, 1800, 'links_request');
			echo $this->Links->returnEnc("[{'status':'ERR','lid':'". $lid ."','msg':'". str_replace("\n","",$GLOBALS['xoopsGTicket']->getErrors()) ."','mode':'request','gticket':'". $new_gticket ."'}]");
			exit();
		}

		if (function_exists('mb_convert_variables')) {
			mb_convert_variables(_CHARSET, 'auto', $_POST);
		}

		$return_msg = $this->_post_common($lid);

		$new_gticket = $GLOBALS['xoopsGTicket']->issue(__LINE__, 1800, 'links_request');
		if (empty($return_msg)) {
			echo $this->Links->returnEnc("[{'status':'OK','lid':'". $lid ."','msg':'". _MD_THANKSFORINFO ."','mode':'request','gticket':'". $new_gticket ."'}]");
		} else {
			echo $this->Links->returnEnc("[{'status':'ERR','lid':'". $lid ."','msg':'". $return_msg ."','mode':'request','gticket':'". $new_gticket ."'}]");
		}

	}

}




